//
//  EditGongGaoViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/10.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "EditGongGaoViewController.h"

@interface EditGongGaoViewController ()<UITextFieldDelegate, UITextViewDelegate>

@property (nonatomic, strong) UITextField *titleTF; // 标题
@property (nonatomic, strong) UITextView *contentTV; // 内容

@end

@implementation EditGongGaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"编辑小组公告";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(editFinish:)];
    rightBarItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 发布
- (void)editFinish:(UIBarButtonItem *)barItem
{
    NSLog(@"编辑完成");
    NSLog(@"标题:%@, 内容:%@", self.titleTF.text, self.contentTV.text);
    
    if (self.titleTF.text.length < 4 || self.titleTF.text.length > 20 || self.contentTV.text.length < 15 || self.contentTV.text.length > 100) {
        [self addAlertNotSuit:@"标题请输入4-20个字，正文请输入15-100个字"];
    } else {
        if (![self addZhengZe:self.titleTF.text] || ![self addZhengZe:self.contentTV.text]) {
            [self addAlertNotSuit:@"标题和正文应该输入汉字|字母|数字|下划线"];
        } else {
            NSLog(@"符合要求");
            NSLog(@"标题：%@， 内容:%@", self.titleTF.text, self.contentTV.text);
        }
    }
}

- (void)addAlertNotSuit:(NSString *)content
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提醒" message:content preferredStyle:UIAlertControllerStyleAlert];
    // 修改标题颜色
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"提醒"];
    [title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [alertVC setValue:title forKey:@"attributedTitle"];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确定");
    }];
    [alertVC addAction:sureAction];
    [self presentViewController:alertVC animated:YES completion:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
    }];
}

#pragma mark - 正则表达式
- (BOOL)addZhengZe:(NSString *)string
{
    NSString *regex = @"^[a-zA-Z0-9\u4e00-\u9fa5,:_]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:string];
    return isMatch;
}

#pragma mark - 布局
- (void)addMySubview
{
    // 顶部灰色条
    UILabel *grayLineOnTop = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15 * kAdjustParamater)];
    grayLineOnTop.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.03];
    [self.view addSubview:grayLineOnTop];
    
    // 灰色条
    UILabel *grayLine1 = [[UILabel alloc] initWithFrame:CGRectMake(0, grayLineOnTop.bottom, kScreenWidth, 2 * kAdjustParamater)];
    grayLine1.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.view addSubview:grayLine1];
    
    // 标题的白色背景
    UIView *titleWhiteBackGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, grayLine1.bottom, kScreenWidth, 50 * kAdjustParamater)];
    titleWhiteBackGroundView.backgroundColor = [UIColor whiteColor];
    titleWhiteBackGroundView.userInteractionEnabled = YES;
    [self.view addSubview:titleWhiteBackGroundView];
    
    // 标题TF
    self.titleTF = [[UITextField alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 10 * kAdjustParamater, 300 * kAdjustParamater, 30 * kAdjustParamater)];
   // self.titleTF.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.titleTF.delegate = self;
    self.titleTF.text = self.editGongGaoModel.gongGaoTitle;
    self.titleTF.tintColor = kCyanColor;
    self.titleTF.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
    self.titleTF.textAlignment = NSTextAlignmentLeft;
    self.titleTF.font = [UIFont systemFontOfSize:k17Big];
    [self.titleTF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventAllEditingEvents];
    [titleWhiteBackGroundView addSubview:_titleTF];
    
    
    UILabel *grayLine2 = [[UILabel alloc] initWithFrame:CGRectMake(0, titleWhiteBackGroundView.bottom, kScreenWidth, 2 * kAdjustParamater)];
    grayLine2.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.view addSubview:grayLine2];
    
    UILabel *grayLine3 = [[UILabel alloc] initWithFrame:CGRectMake(0, grayLine2.bottom, kScreenWidth, 15 * kAdjustParamater)];
    grayLine3.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.03];
    [self.view addSubview:grayLine3];
    
    UILabel *grayLine4 = [[UILabel alloc] initWithFrame:CGRectMake(0, grayLine3.bottom, kScreenWidth, 2 * kAdjustParamater)];
    grayLine4.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.view addSubview:grayLine4];
    
    // 内容背景
    UIView *contentWhiteGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, grayLine4.bottom, kScreenWidth, 170 * kAdjustParamater)];
    contentWhiteGroundView.backgroundColor = [UIColor whiteColor];
    contentWhiteGroundView.userInteractionEnabled = YES;
    [self.view addSubview:contentWhiteGroundView];
    
    // 内容TV
    self.contentTV = [[UITextView alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 5 * kAdjustParamater, 345 * kAdjustParamater, 130 * kAdjustParamater)];
    self.contentTV.text = self.editGongGaoModel.content;
    // self.contentTV.clearsContextBeforeDrawing = YES;
 //   self.contentTV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.contentTV.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
    self.contentTV.tintColor = kCyanColor;
    self.contentTV.delegate = self;
    self.contentTV.scrollEnabled = NO;
    self.contentTV.font = [UIFont systemFontOfSize:k15Big];
    [contentWhiteGroundView addSubview:_contentTV];
    
    // 添加TV辅助视图
    // 构建辅助视图
    UIView *seorryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 25 * kAdjustParamater)];
    seorryView.backgroundColor = [UIColor colorWithRed:249 / 255.0 green:249 / 255.0 blue:249 / 255.0 alpha:1];
    
    UILabel *grayLineOnSeorryView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1 * kAdjustParamater)];
    grayLineOnSeorryView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [seorryView addSubview:grayLineOnSeorryView];
    
    // 完成button
    UIButton *textViewFinishButton = [UIButton buttonWithType:UIButtonTypeSystem];
    textViewFinishButton.frame = CGRectMake(334  *kAdjustParamater, 5 * kAdjustParamater, 30 * kAdjustParamater, 15 * kAdjustParamater);
    textViewFinishButton.layer.cornerRadius = 2 * kAdjustParamater;
    [textViewFinishButton setTitle:@"完成" forState:UIControlStateNormal];
    [textViewFinishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    textViewFinishButton.backgroundColor = kCyanColor;
    textViewFinishButton.titleLabel.font = [UIFont systemFontOfSize:k13Big];
    [textViewFinishButton addTarget:self action:@selector(tvFinish:) forControlEvents:UIControlEventTouchUpInside];
    [seorryView addSubview:textViewFinishButton];
    self.contentTV.inputAccessoryView = seorryView;
    
    UILabel *grayLine5 = [[UILabel alloc] initWithFrame:CGRectMake(0, contentWhiteGroundView.bottom, kScreenWidth, 2 * kAdjustParamater)];
    grayLine5.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.view addSubview:grayLine5];
    
    UILabel *grayLineOnBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, grayLine5.bottom, kScreenWidth, 400 * kAdjustParamater)];
    grayLineOnBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.03];
    [self.view addSubview:grayLineOnBottom];
    
    
    
}

#pragma mark - textfield内容变化回调
- (void)textChange:(UITextField *)textField
{
    NSLog(@"textField = %@", textField.text);
    //    if (textField.text.length == 0) {
    //        textField.text = @"标题  (必填)  ,  4-20个字";
    //    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self.titleTF resignFirstResponder];
}

- (void)textViewDidChange:(UITextView *)textView
{
    NSLog(@"textView = %@", textView.text);
    //    if (textView.text.length == 0) {
    //        textView.text = @"正文 (必填)  ,  15-100个字";
    //    }
}



#pragma mark - 回收键盘
- (void)tvFinish:(UIButton *)button
{
    NSLog(@"回收键盘");
    [self.contentTV resignFirstResponder];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
